import axios from "../../utils/axios";
const app = getApp();

var imgupload = require('../../utils/pets/imgupload/imgupload');

Page({
  data: {
    array: ['是', '否', '不知道'],
    userdi: ['小于35cm', '大于35cm'],
    typei: ['狗狗', '猫咪'],
    sexs: ['男', '女'],
    date: '请选择宝贝出生日期:',
    userName: '',
    marks: '',
    sexsindex: 0,
    userdiind: 0,
    typeiind: 0,
    userditext: '',
    bindPickindex: 3,
    colcos: '',
    varietiesname: '请选择宝贝的品种',
    varietiesnameid: 0,
    uploadData: {
      images: [],
      onPhotoDelete: "bindPhotoDelete",
      onChooseImage: "bindChooseImage"
    },
    imgtrue: 0,
    arrayimgss: '',
    modifyContac: '',
    usernameValue: '',
    colourValue: '',
    markValue: '',
    pets_id: 0,
  },
  onLoad: function (options) {
    this.data.isfist = true
    imgupload(this).init();

    console.log(options)
    if (options.modifyContac == '2') {

      axios('/personal/petsDetail', {
        pets_id: options.contact_id
      }).then(res => {
        console.log(res.data.result)
        this.setData({
          modifyContac: options.modifyContac,
          arrayimgss: res.data.result.sculpture,
          usernameValue: res.data.result.pet_name,
          userName: res.data.result.pet_name,
          varietiesname: res.data.result.breed_name,
          varietiesnameid: res.data.result.breed_id,
          date: res.data.result.birthday,
          colourValue: res.data.result.color || "",
          markValue: res.data.result.mark || "",
          pets_id: options.contact_id
        })
      })

    }

  },
  // 点击图片
  formResetss: function (e) {
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9
      // sizeType: ['compressed'],
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      compressed: false,
      success: function (res) {
        console.log(res)
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths;
        console.log('>>>', tempFilePaths[0]);
        // that.data.array[id].src = tempFilePaths[0];

        that.setData({
          arrayimgss: tempFilePaths[0],
          imgtrue: 1
        })
        console.log(that.data.array);
      }
    });

  },
  onShow: function () {
    if (app.globalData.allpets && !this.data.isfist && app.globalData.allpets.is_refresh) {
      app.globalData.allpets.is_refresh = false
      var shops = app.globalData.allpets
      console.log('app.globalData.addAddr>>>', app.globalData.allpets)

      this.setData({
        varietiesname: shops.allpetsname,
        varietiesnameid: shops.allpetsid
      })


    }

    this.data.isfist = false
  },
  formSubmit: function (e) {
    console.log('form发生了submit事件，提交数据：', e.detail.value)
  },
  formReset: function () {
    console.log('form发生了reset事件')
  },
  bindPickerChange: function (e) {
    this.setData({
      bindPickindex: e.detail.value
    })
  },
  userNameInput: function (e) {
    this.setData({
      userName: e.detail.value
    })
  },
  colcosInput: function (e) {
    this.setData({
      colcos: e.detail.value
    })
  },
  onChangeTap: function (e) {

    this.setData({
      sexsindex: e.currentTarget.dataset.index
    })
  },
  Label: function (e) {
    this.setData({
      userdiind: e.currentTarget.dataset.index,
      userditext: e.currentTarget.dataset.userditext
    })
  },
  typei: function (e) {
    this.setData({
      typeiind: e.currentTarget.dataset.index,

    })
  },
  Varieties: function (e) {
    wx.navigateTo({
      url: `/pages/allpets/allpets?type=${this.data.typeiind+1}`
    })
  },
  markInput: function (e) {
    this.setData({
      marks: e.detail.value
    })
  },
  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },
  // 确定上传添加
  baocuns: function (e) {
    let userName = this.data.userName //姓名
    let sexsindex = this.data.sexsindex //性别
    let date = this.data.date //生日 
    let colcos = this.data.colcos //毛色 
    let userditext = this.data.userditext //身高 
    let bindPickindex = this.data.bindPickindex //是否绝育 
    let marks = this.data.marks //备注
    let breed_id = this.data.varietiesnameid

    if (this.data.arrayimgss == '') {
      wx.showToast({
        title: `请上传图片`,
        icon: 'success',
        duration: 2000
      })
      return
    }
    if (userName == '') {
      wx.showToast({
        title: `请填写宠物名字`,
        icon: 'success',
        duration: 2000
      })
      return
    }
    if (breed_id == 0) {
      wx.showToast({
        title: `请填写品种`,
        icon: 'success',
        duration: 2000
      })
      return
    }

    const that = this;

    if (that.data.modifyContac == '2') {
      // 选择图片的上传
      if (that.data.imgtrue == 1) {
        let tokens = wx.getStorageSync('token')

        wx.uploadFile({
          url: 'https://api.petintech.com/user/modifyPet', //开发者服务器的 url
          filePath: that.data.arrayimgss, // 要上传文件资源的路径 String类型！！！
          name: 'file', // 文件对应的 key ,(后台接口规定的关于图片的请求参数)
          header: {
            'content-type': 'multipart/form-data',
            ["X-TOKEN"]: tokens
          }, // 设置请求的 header
          formData: {
            pets_id: this.data.pets_id,
            name: userName,
            gendor: (sexsindex + 1),
            birthday: date,
            breed_id: breed_id,
            color: colcos,
            height: userditext,
            sterilization: bindPickindex,
            mark: marks,

          }, // HTTP 请求中其他额外的参数
          success: function (res) {
            console.log(res);

            wx.showToast({
              title: res.data.message,
              icon: 'success',
              duration: 2000
            })
            wx.navigateBack({
              delta: 1
            })

          },
          fail: function (res) {
            console.log("图片上传失败" + res);
          }
        })
      } else {
        axios('/personal/modifyPet', {
          pets_id: this.data.pets_id,
          name: userName,
          gendor: (sexsindex + 1),
          birthday: date,
          breed_id: breed_id,
          color: colcos,
          height: userditext,
          sterilization: bindPickindex,
          mark: marks,
        }).then(res => {
          console.log('sss>>>>>', res.data)
          wx.showToast({
            title: res.data.message,
            icon: 'success',
            duration: 2000
          })
          wx.navigateBack({
            delta: 1
          })
        })
      }

    } else {
      let tokens = app.globalData.userToken
      wx.uploadFile({
        url: 'https://api.petintech.com/user/appendPet', //开发者服务器的 url
        filePath: that.data.arrayimgss, // 要上传文件资源的路径 String类型！！！
        name: 'file', // 文件对应的 key ,(后台接口规定的关于图片的请求参数)
        header: {
          'content-type': 'multipart/form-data',
          ["X-TOKEN"]: tokens
        }, // 设置请求的 header
        formData: {
          name: userName,
          gendor: (sexsindex + 1),
          birthday: date,
          breed_id: breed_id,
          color: colcos,
          height: userditext,
          sterilization: bindPickindex,
          mark: marks,

        }, // HTTP 请求中其他额外的参数
        success: function (res) {
          console.log(res);

          wx.showToast({
            title: res.data.message,
            icon: 'success',
            duration: 2000
          })
          wx.navigateBack({
            delta: 1
          })

        },
        fail: function (res) {
          console.log("图片上传失败" + res);
        }
      })

    }

  },
})